from flask_script import Manager
from flask_migrate import MigrateCommand,Migrate
from exts import db
from dxbbs import app
from apps.cms import models as cms_models
from apps.front import models as front_modles
from apps.models import BannerModel,BoardModel,PostModel

CMSUser = cms_models.CMSUser
CMSRole = cms_models.CMSRole
FrontUser = front_modles.FrontUser
CMSPermission = cms_models.CMSPermission

manager = Manager(app)
#绑定app 和db
migrate = Migrate(app,db,compare_type=True)
#将Migratecommand命令添加到manager
manager.add_command('db',MigrateCommand)

@manager.option('-u','--username',dest='username')
@manager.option('-p','--password',dest='password')
@manager.option('-e','--email',dest='email')
def create_cms_user(username,password,email):
    user = CMSUser(username=username,password=password,email=email)
    db.session.add(user)
    db.session.commit()
    print('cms用户添加成功！')

@manager.option('-e','--email',dest='email')
def delete_cms_user(email):
    user = CMSUser.query.filter_by(email=email).first()
    db.session.delete(user)
    db.session.commit()
    print('cms用户删除成功！')

@manager.command
def create_role():
    #1.访问者
    visitor = CMSRole(name='访问者',desc='最底层的人！')
    visitor.permissions = CMSPermission.VISTOR
    #2.运营（修改个人个人信息，管理帖子，管理评论，管理前台用户）
    operator = CMSRole(name='运营',desc='有点权力干活的人！')
    operator.permissions = CMSPermission.VISTOR|CMSPermission.POSTER|CMSPermission.COMMENTER|CMSPermission.FRONTUSER
    #3.管理员
    admin = CMSRole(name='管理员',desc='有点权力就随便删我的帖子？拥有所有权限！')
    admin.permissions = CMSPermission.VISTOR|CMSPermission.POSTER|CMSPermission.COMMENTER|CMSPermission.FRONTUSER|CMSPermission.BOARDER
    #4.开发者
    developer = CMSRole(name='开发者',desc='来来来，代码你写的你说了算！')
    developer.permissions = CMSPermission.ALL_PERMISSION

    db.session.add_all([visitor,operator,admin,developer])
    db.session.commit()
@manager.option('-e','--email',dest='email')
@manager.option('-n','--name',dest='name')
def add_user_to_role(email,name):
    user = CMSUser.query.filter_by(email=email).first()
    if user:
        role = CMSRole.query.filter_by(name=name).first()
        if role:
            role.users.append(user)
            db.session.commit()
            print('用户添加到角色成功！')
        else:
            print('该角色不存在！！！')
    else:
        print('该邮箱账户不存在！')

@manager.command
def test_permission():
    user = CMSUser.query.first()
    if user.has_permission(CMSPermission.VISTOR):
        print('用户具有访问者权限')
    else:
        print('用户不具有访问者权限！')
@manager.command
def add_test_posts():
    for i in range(1,205):
        title = '标题:%s' % i
        content = '内容:%s' % i
        board = BoardModel.query.first()
        author = FrontUser.query.first()
        post = PostModel(title=title,content=content)
        post.board = board
        post.author = author
        db.session.add(post)
        db.session.commit()
    print('测试帖子数据添加成功！')
@manager.option('-e','--email',dest='email')
def test_deve_per(email):
    user = CMSUser.query.filter_by(email=email).first()
    if user:
        if user.is_developer:
            print('给大佬开发者递茶！')
        else:
            print('你又不是开发者，一边去！')
    else:
        print('用户不存在！')
@manager.option('-t','--telephone',dest='telephone')
@manager.option('-p','--password',dest='password')
@manager.option('-u','--username',dest='username')
def create_front_user(telephone,password,username):
    user = FrontUser(telephone=telephone,password=password,username=username)
    db.session.add(user)
    db.session.commit()
    print('前台测试用户添加成功！')

if __name__ == '__main__':
    manager.run()